package com.yang.controller.wx;

import com.yang.bean.*;
import com.yang.bean.bj.vo.CartVO;
import com.yang.bean.bj.vo.CheckOutVO;
import com.yang.bean.bj.vo.ProductIdVO;
import com.yang.bean.common.BaseRespVo;
import com.yang.exception.WXException;
import com.yang.service.wx.CartServiceWX;
import com.yang.utils.UserUtil;
import org.apache.shiro.session.Session;
import org.apache.shiro.web.session.mgt.DefaultWebSessionManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @description: TOOD
 * @return:
 * @author: BaoZi
 * @date: 2022/9/9
 */
@RestController
@RequestMapping("wx/cart")
public class WXCartController {

    @Autowired
    CartServiceWX cartServiceWX;

    @Autowired
    DefaultWebSessionManager defaultWebSessionManager;



    /**
     *购物车->index显示购物车
     * @param request
     * @return com.yang.bean.common.BaseRespVo
     * @author BaoZi
     * @since 2022/09/11 11:39
     */
    @RequestMapping("index")
    public BaseRespVo index(HttpServletRequest request){
        //需要获取登录的用户信息
        String header = request.getHeader("X-CskaoyanMarket-Token");
        //需要获取登录的用户信息
        MarketUser marketUser =null;
        try {
            Session session = defaultWebSessionManager.getSession(() -> header);
            marketUser = (MarketUser) session.getAttribute(header);
        } catch (Exception e) {
            throw new WXException("session丢失");
        }
        //获取用户id
        CartVO cartVO = cartServiceWX.queryCart(marketUser.getId());
        return BaseRespVo.ok(cartVO);
    }


    /**
     *购物车->add 添加商品到购物车
     * @param marketCart
     * @param request
     * @return com.yang.bean.common.BaseRespVo
     * @author BaoZi
     * @since 2022/09/11 11:40
     */
    @RequestMapping("add")
    public BaseRespVo add(@RequestBody MarketCart marketCart, HttpServletRequest request){

        //需要获取登录的用户信息
        String header = request.getHeader("X-CskaoyanMarket-Token");
        //需要获取登录的用户信息
        MarketUser marketUser =null;
        try {
            Session session = defaultWebSessionManager.getSession(() -> header);
            marketUser = (MarketUser) session.getAttribute(header);
        } catch (Exception e) {
            throw new WXException("session丢失");
        }

       Integer date =  cartServiceWX.insertCart(marketCart,marketUser.getId());
        return BaseRespVo.ok(date);
    }

    /**
     *购物车->fastadd立即购买
     * @param
     * @param request
     * @return com.yang.bean.common.BaseRespVo
     * @author BaoZi
     * @since 2022/09/11 22:04
     */
    @RequestMapping("fastadd")
    public BaseRespVo fastadd(@RequestBody MarketCart marketCart, HttpServletRequest request){
        //需要获取登录的用户信息
        String header = request.getHeader("X-CskaoyanMarket-Token");
        //需要获取登录的用户信息
        MarketUser marketUser =null;
        try {
            Session session = defaultWebSessionManager.getSession(() -> header);
            marketUser = (MarketUser) session.getAttribute(header);
        } catch (Exception e) {
            throw new WXException("session丢失");
        }
        marketCart.setUserId(marketUser.getId());
        //返回订单总数
        Integer date = cartServiceWX.addCart(marketCart,marketUser.getId());
        return BaseRespVo.ok(date);
    }


    /**
     *购物车->goodcount获取购物车商品件数
     * @return com.yang.bean.common.BaseRespVo
     * @author BaoZi
     * @since 2022/09/11 22:05
     */
    @RequestMapping("goodscount")
    public BaseRespVo goodsCount(HttpServletRequest request){
        MarketUser userMsg = UserUtil.getUserMsg(request,defaultWebSessionManager);
        Integer date = cartServiceWX.queryCountCart(userMsg.getId());
        return BaseRespVo.ok(date);
    }

    /**
     *购物车->update刷新购物车
     * @param marketCart
     * @return com.yang.bean.common.BaseRespVo
     * @author BaoZi
     * @since 2022/09/11 22:06
     */
    @RequestMapping("update")
    public BaseRespVo update(@RequestBody MarketCart marketCart){
        cartServiceWX.updateCartById(marketCart);
        return BaseRespVo.ok();
    }


    /**
     *购物车->delete删除购物车商品
     * @param productIdVO
     * @return com.yang.bean.common.BaseRespVo
     * @author BaoZi
     * @since 2022/09/11 22:15
     */
    @RequestMapping("delete")
    public BaseRespVo delete(@RequestBody ProductIdVO productIdVO,HttpServletRequest request){
        //需要获取登录的用户信息
        String header = request.getHeader("X-CskaoyanMarket-Token");
        //需要获取登录的用户信息
        MarketUser marketUser =null;
        try {
            Session session = defaultWebSessionManager.getSession(() -> header);
            marketUser = (MarketUser) session.getAttribute(header);
        } catch (Exception e) {
            throw new WXException("session丢失");
        }
        Integer userId = marketUser.getId();
        CartVO cartVO = cartServiceWX.deleteCartById(productIdVO,userId);
        return BaseRespVo.ok(cartVO);
    }


    /**
     *购物车->checked选择或取消商品
     * @param productIdVO 
     * @param request 
     * @return com.yang.bean.common.BaseRespVo
     * @author BaoZi
     * @since 2022/09/12 13:42 
     */
    @RequestMapping("checked")
    public BaseRespVo checked(@RequestBody ProductIdVO productIdVO,HttpServletRequest request){
        MarketUser userMsg = UserUtil.getUserMsg(request,defaultWebSessionManager);
        CartVO cartVO = cartServiceWX.checkedGoods(productIdVO,userMsg.getId());
        return BaseRespVo.ok(cartVO);
    }

    /**
     *购物车-> checkOut下单前确认信息
     * @param checkOut
     * @return com.yang.bean.common.BaseRespVo
     * @author BaoZi
     * @since 2022/09/13 11:17
     */
    @RequestMapping("checkout")
    public BaseRespVo checkOut(CheckOutVO checkOut){
        CheckOutVO checkOutVO = cartServiceWX.queryCheckOut(checkOut);
        return BaseRespVo.ok(checkOutVO);

    }

}
